﻿////////////////////////////////////////////
/*
 *本代码由代码生成器自动生成，请不要更改此文件的任何代码。如需要进行修改，请在对应的扩展类中添加新类，并使用partial修饰符。
 *生成时间：2015-03-30
 *生成者：JerzyJiang
 */
////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using Model;
using DBUtility;
using CommonUtility;

namespace DAL
{
	public partial class Permission
	{	
		/// <summary>
		/// 获取符合条件记录的条数
		/// </summary>
		/// <param name="_condition">条件语句，不包含“where”</param>
		/// <returns></returns>
		public static IList<PermissionView> GetList(string _condition)
		{    
			IList<PermissionView> list = new List<PermissionView>();
			try
			{
				string strSQL = @"
SELECT 
	[userID],
	[actID],
	[funID],
	[roleID]
FROM [vPermission] WHERE 1=1 ";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " AND " + _condition;
				
                DataSet mySet = new DataSet();
                SqlHelper.FillDataset(strSQL, mySet, "data", true);
                if (mySet != null && mySet.Tables.Count > 0)
                {
                    foreach (DataRow dr in mySet.Tables["data"].Rows)
                    {
						PermissionView model = new PermissionView();
						model.UserID = Common.ToString(dr["userID"]);
						model.ActID = Common.ToString(dr["actID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.RoleID = Common.ToString(dr["roleID"]);
						list.Add(model);
					}
				}
				mySet.Dispose();
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Permission.cs/GetList("+_condition+")", ex);
			}
			return list;	
		}
		
		/// <summary>
		/// 获取符合条件记录的条数
		/// </summary>
		/// <param name="_condition">条件语句，不包含“where”</param>
		/// <param name="_parms">条件中的参数</param>
		/// <returns></returns>
		public static IList<PermissionView> GetList(string _condition, SqlParameter[] _parms)
		{    
			IList<PermissionView> list = new List<PermissionView>();
			try
			{
				string strSQL = @"
SELECT 
	[userID],
	[actID],
	[funID],
	[roleID]
FROM [vPermission]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " AND " + _condition;
				
                DataSet mySet = new DataSet();
                SqlHelper.FillDataset(strSQL, mySet, "data", _parms, true);
                if (mySet != null && mySet.Tables.Count > 0)
                {
                    foreach (DataRow dr in mySet.Tables["data"].Rows)
                    {
						PermissionView model = new PermissionView();
						model.UserID = Common.ToString(dr["userID"]);
						model.ActID = Common.ToString(dr["actID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.RoleID = Common.ToString(dr["roleID"]);
						list.Add(model);
					}
				}
				mySet.Dispose();
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Permission.cs/GetList("+_condition+")", ex);
			}
			return list;	
		}		
		
		/// <summary>
        /// 根据条件语句取得符合条件的数据表
        /// </summary>
        /// <param name="_condition">条件语句，不包含“where”</param>
        /// <returns></returns>
        public static DataTable GetTable(string _condition)
        {
            DataSet mySet = new DataSet();
			try
			{
				string strSQL = @"
SELECT 
	[userID],
	[actID],
	[funID],
	[roleID]
FROM [vPermission]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " AND " + _condition;
				
                SqlHelper.FillDataset(strSQL, mySet, "data", true);
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Permission.cs/GetTable("+_condition+")", ex);
			}
			return mySet!=null && mySet.Tables.Count>0 ? mySet.Tables[0] : null;	
        }
        
        /// <summary>
        /// 获取符合条件记录的条数。
		/// 在使用SQL分页是可以用到，与getTable配合使用
        /// </summary>
        /// <param name="_condition">条件语句，不包含“where”</param>
        /// <returns></returns>
        public static int Count(string _condition)
        {
			int result = 0;
			try
			{
				string strSQL = "SELECT COUNT(*) FROM [vPermission]";
				if (!string.IsNullOrEmpty(_condition))
					strSQL += " AND " + _condition;
				
                result = Common.ToInt32(SqlHelper.ExecuteScalar(strSQL));
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Permission.cs/Count("+_condition+")", ex);
			}
			return result;	
        }
		
        /// <summary>
        /// 获取分页数据（数据库分页）
        /// </summary>
        /// <param name="_pageIndex">页码，从1开始</param>
        /// <param name="_pageSize">分页大小（每页显示记录数）</param>
        /// <param name="_condition">条件语句，不包含“where”（在组装条件前需要对条件值进行单引号处理）</param>
        /// <param name="_orderStr">排序条件，如：ID DESC</param>
        /// <param name="_total">返回符合条件的总记录数</param>
        /// <param name="_message">存储过程的执行结果</param>
        /// <returns>当前页的数据表，没有数据返回Null</returns>
		public static DataTable GetPageData(int _pageIndex, int _pageSize, string _condition, string _orderStr, ref int _total, ref string _message)
		{
			int recordCount = 0; //本次查到返回的记录数目
			int pageCount = 0; //返回本次查询能够返回的页数
			if (string.IsNullOrEmpty(_orderStr))
				_orderStr = "RID DESC";

			return SqlHelper.GetPageData("vPermission",0,1,_condition,_orderStr,"*",_pageIndex,_pageSize,ref _total,ref recordCount, ref pageCount, ref _message);
		}
		
		/// <summary>
        /// 获取分页数据对象（数据库分页）
        /// </summary>
        /// <param name="_pageIndex">页码，从1开始</param>
        /// <param name="_pageSize">分页大小（每页显示记录数）</param>
        /// <param name="_condition">条件语句，不包含“where”（在组装条件前需要对条件值进行单引号处理）</param>
        /// <param name="_orderStr">排序条件，如：ID DESC</param>
        /// <param name="_total">返回符合条件的总记录数</param>
        /// <param name="_message">存储过程的执行结果</param>
        /// <returns>当前页的数据对象集合</returns>
		public static IList<PermissionView> GetPageObject(int _pageIndex, int _pageSize, string _condition, string _orderStr, ref int _total, ref string _message)
		{
			IList<PermissionView> list = new List<PermissionView>();
			try
			{
				if (string.IsNullOrEmpty(_orderStr))
					_orderStr = "RID DESC";
				int recordCount = 0; //本次查到返回的记录数目
				int pageCount = 0; //返回本次查询能够返回的页数
				DataTable td = SqlHelper.GetPageData("vPermission",0,1,_condition,_orderStr,"*",_pageIndex,_pageSize,ref _total,ref recordCount, ref pageCount, ref _message);
				if (td != null)
				{
					foreach (DataRow dr in td.Rows)
					{
						PermissionView model = new PermissionView();
						model.UserID = Common.ToString(dr["userID"]);
						model.ActID = Common.ToString(dr["actID"]);
						model.FunID = Common.ToString(dr["funID"]);
						model.RoleID = Common.ToString(dr["roleID"]);
						list.Add(model);
					}
				}
			}
			catch (Exception ex)
			{
				Common.Application_Error_Server("Permission.cs/GetPageData(2)", ex);
			}

			return list;
		}
	}
}
